'use strict';

import React, {
    Component,
    PropTypes
} from 'react';
import {
    Link,
    hashHistory
} from 'react-router';
import usrimg from '../img/user.png'
import {
    HttpService,
    Toast
} from '../Http';

import {
    R_header
} from './common/index'

export default class Demo extends React.Component {
    constructor(props, context) {
        super(props, context);
        this.state = {
            list: []
        }
    }
    componentWillMount() {
        if (localStorage.attback == 1) {
            delete localStorage.attback
            history.back(-1)
            return
        }
        localStorage.clear();
        localStorage.publicLink = true
        localStorage.appToken = this.props.params.token
        HttpService.query({
            url: '/app/object/getBabysJxt',
            data: {
                token: localStorage.appToken,
                weixinclient: 'true'
            },
            success: (res => {
                if (res.code == 10020) {
                    if (res.data.length == 1) {
                        location.href = '#/APP/' + res.data[0].babyid + '/true'
                        return;
                    }
                    this.setState({
                        list: res.data
                    })
                } else if (res.code == 10019) {
                    Toast.toast('无宝贝信息，去添加设备');
                    hashHistory.push('/addDevice');
                } else {
                    Toast.toast('账户登录错误或过期', 0);
                }
            })
        })
    }

    render() {
        let listItem = this.state.list.map((json, index) => {
            return (
                <Link className="device-info block" to={'/App/' + json.babyid + '/false'}>
                    <div className="headimg">
                        {!json.headimg
                            ? <img src={usrimg}/>
                            : <img src={"/media" + json.headimg}/>
}
                    </div>
                    <div className="info">
                        <div className="name">{json.babyname}</div>
                        <div className="time">设备套餐有效期：{json.deviceType == 3
                                ? '永久有效'
                                : json.endtime.replace(/\-/g, '.')}</div>
                    </div>
                </Link>
            )
        });
        return (
            <div>
                <div className="layer_content2">
                    {listItem}
                </div>
            </div>
        )
    }
}
